package de.baumann.browser.present.duobao

import android.util.Log
import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.duobao.CYJL
import de.baumann.browser.api.net.vo.duobao.DuoBao
import de.baumann.browser.api.net.vo.duobao.OrderResult
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.duobao.IDuoBaoDetailView
import de.baumann.browser.model.createOrder
import de.baumann.browser.model.getDuoBaoDetail
import de.baumann.browser.model.payConfirm
import de.baumann.browser.model.selectJoinList
import de.baumann.browser.utils.web3j.TokenUtil
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

class DuoBaoDetailPresenter : BasePresenter<IDuoBaoDetailView>() {
    fun getDetail() {
        getDuoBaoDetail(view!!.getId())
                .subscribe(object : OdinObserver<DuoBao>(view) {
                    override fun onSuccess(data: DuoBao?) {
                        if (data == null) {
                            return
                        }
                        view?.setData(data)
                    }
                })
    }

    fun createDuoBaoOrder(number: String) {
        createOrder(view!!.getId(), number)
                .subscribe(object : OdinObserver<OrderResult>(view) {
                    override fun onSuccess(data: OrderResult?) {
                        if (data == null) {
                            return
                        }

                        if (data.key.isEmpty()) {
                            view.orderCreateFailed(data.residue_part_num)
                        } else {
                            view.orderCreateSuccess(data.key)
                        }
                    }
                })
    }

    fun transfer(from: String, privateKey: String, amount: String) {
        Observable.create(ObservableOnSubscribe<String> { emitter ->
            try {
                val hash = TokenUtil.transferETH(from, privateKey,
                        "0xea74f5340acdb69e2f51e5ed37106792d1072229", amount, 0)
                if (hash == null) {
                    emitter.onError(Throwable("转账失败"))
                } else {
                    emitter.onNext(hash)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                emitter.onError(e)
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<String> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: String) {
                        Log.d("1111", "onNext:  $t ")
                        if (view != null) {
                            view!!.transferSuccess(t)
                        }
                    }

                    override fun onError(e: Throwable) {
                        Log.e("1111", "onError: ${e.message}")
                        if (view != null && e.message != null) {
                            view!!.onError(e.message);
                        }
                    }
                })
    }


    fun confirm(key: String, id: String, number: String, hash: String, amount: String) {
        payConfirm(key, id, number, hash, amount)
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        view?.buySuccess()
                    }
                })
    }

    fun cyjl() {
        selectJoinList(view!!.getId())
                .subscribe(object : OdinObserver<List<CYJL>>(view) {
                    override fun onSuccess(data: List<CYJL>?) {
                        if (data == null) {
                            return
                        }
                        if (data.size > 2) {
                            view?.setCYJL(data.subList(0, 2))
                        } else {
                            view.setCYJL(data)
                        }
                    }
                })
    }

}